package com.yc.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.yc.bean.Addr;
import com.yc.biz.AddrBiz;
import com.yc.biz.impl.AddrBizImpl;

@WebServlet("/addrServlet")
public class AddrServlet extends HttpServlet {

	AddrBiz ab=new AddrBizImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter out=resp.getWriter();
		String op=req.getParameter("op");
		
		if(op.equals("getAllAddr")){
			getAllAddr(req,resp,out);
		}else if(op.equals("setDefault")){
			setDefault(req,resp,out);
		}else if(op.equals("addAddress")){
			addAddress(req,resp,out);
		}else if(op.equals("getDafalutAddr")){
			getDafalutAddr(req,resp,out);
		}else if(op.equals("getProv")){
			getProv(req,resp,out);
		}else if(op.equals("getCity")){
			getCity(req,resp,out);
		}else if(op.equals("getArea")){
			getArea(req,resp,out);
		}
		
	}
	
	//获得区域
	private void getArea(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int Ccode = 0;
		try {
			Ccode = Integer.parseInt( req.getParameter("Ccode") );
		} catch (NumberFormatException e) {
			e.printStackTrace();
			out.print(-1);
		}
		List<Map<String,String>> list = ab.getArea(Ccode);
		System.out.println( list );
		req.getSession().setAttribute("area", list);
		Gson gson = new Gson();
		out.print(gson.toJson(list));
	}

	//获得各省城市
	private void getCity(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int pcode = 0;
		try {
			pcode = Integer.parseInt(req.getParameter("pcode"));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			out.print(-1);
		}
		List<Map<String,String>> list = ab.getCity(pcode);
		req.getSession().setAttribute("city", list);
		Gson gson = new Gson();
		out.print(gson.toJson(list));
	}

	//获取全国省份
	private void getProv(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		List<Map<String,String>> list = ab.getProv();
		if(list.isEmpty()){
			out.print(-1);
			return;
		}
		Gson gson = new Gson();
		req.getSession().setAttribute("prov", list);
		out.print(gson.toJson(list));
	}

	//获取默认地址 并进行显示
	private void getDafalutAddr(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid=Integer.parseInt( req.getSession().getAttribute("uid")+"" );
		List<Map<String,String>> list=ab.getDafalutAddr(uid);
		if(list.isEmpty()){
			out.print(0);
			return;
		}
		Gson gson=new Gson();
		req.getSession().setAttribute("aid", Integer.parseInt(list.get(0).get("aid")+""));
		out.print(gson.toJson(list));
	}

	//添加地址
	private void addAddress(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid=Integer.parseInt( req.getSession().getAttribute("uid")+"" );
		String uname=req.getParameter("uname");
		String prov=req.getParameter("prov");
		String city=req.getParameter("city");
		String area=req.getParameter("area");
		String addr=req.getParameter("addr");
		String tel=req.getParameter("tel");
		
		int result=ab.addAddr(uid, uname, tel, prov, city, area, addr);
		if(result>0){
			out.print(1);
		}else{
			out.print(0);
		}
	}

	//设置为默认地址
	private void setDefault(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid = 0;
		int aid = 0;
		try {
			uid = Integer.parseInt(req.getSession().getAttribute("uid")+"");
			aid = Integer.parseInt(req.getParameter("aid"));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			out.print(-1);
		}
		
		int result=ab.setDefault(uid, aid);
		System.out.println(result);
		if(result>0){
			out.print(1);
		}else{
			out.print(1);
		}
	}

	//查询所有地址信息
	private void getAllAddr(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid = 0;
		try {
			uid = Integer.parseInt(req.getSession().getAttribute("uid")+"");
		} catch (NumberFormatException e) {
			e.printStackTrace();
			out.print(-1);
			return;
		}
		List<Map<String, String>> list=ab.findAllAddr(uid);
		if(list.isEmpty()){
			out.print(0);
			return;
		}
		Gson gson=new Gson();
		out.print(gson.toJson(list));
	}

}
